<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
        content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no,minimal-ui">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>轮播图</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            width: 566px;
            height: 333px;
            overflow: hidden;
            margin: 100px auto;
            position: relative;
        }

        .box>img {
            width: 566px;
            height: 333px;
        }

        ul {
            position: absolute;
            list-style: none;
            overflow: hidden;
            bottom: 20px;
            left: 50%;
            transform: translateX(-50%);
        }

        li {
            float: left;
            width: 10px;
            height: 10px;
            background-color: papayawhip;
            border-radius: 50%;
            color: powderblue;
            margin-left: 10px;
        }
    </style>
</head>

<body>
    <div class="box">
        <img src="images/1.jpg" alt="">
        <img src="images/2.jpg" alt="">
        <img src="images/3.jpg" alt="">
        <img src="images/4.jpg" alt="">
        <ul>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>
</body>
<script>
    var imgs = document.querySelectorAll(".box img"); //获取到所有图片
    var btn = document.querySelectorAll(".box li"); //获取小圆点
    var num = 0;
    btn[num].style.backgroundColor = "red";
    var timer = setInterval(roll, 1000)

    function roll() {
        num++;
        num >= imgs.length ? num = 0 : num = num;
        imgs.forEach((item, index) => {
            item.style.display = 'none'
            btn[index].style.background = 'papayawhip'
        })
        imgs[num].style.display = 'block' //显示相对应的图片
        btn[num].style.background = 'red'
    }



    btn.forEach(function (item, index) {
        item.onclick = function () {
            clearInterval(timer)
            num = index - 1
            roll()
        }
        // item.onmouseleave = function () {
        //   timer = setInterval(roll, 2000)
        // }
    })
</script>

</html>